#![allow(dead_code)]
use crate::domain::CountNum;
use crate::domain::CustOrderBean;
use crate::domain::OrderItemBean;
use crate::domain::CustOrderQueryRequest;
use crate::serviceimpl::init_db;
use crate::serviceimpl::order_item_service::validate_order_items as service_validate_order_items;
use crate::utils::string_util::is_digit;
use crate::utils::string_util::right_find_under_line;
use sqlx::Error;
use substring::Substring;

pub async fn activate_all_cust_orders(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let cust_orders = &app_state.await.context.cust_orders;
	let vecter_iterator = ids.iter();
	for cust_order_id in vecter_iterator {
		let _ = cust_orders.activate_cust_order(*cust_order_id).await;
	}
	Ok(0)
}

pub async fn activate_cust_order(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.activate_cust_order(id).await
}

pub async fn add_cust_order(cust_order:CustOrderBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.add_cust_order(cust_order).await
}

pub async fn clone_all_cust_orders(ids:Vec<i64>) ->Result<u64, Error> {
	let vecter_iterator = ids.iter();
	for cust_order_id in vecter_iterator {
		let _ = clone_cust_order(*cust_order_id).await;
	}
	Ok(0)
}

pub async fn clone_cust_order(id:i64) ->Result<u64, Error> {
	let mut cust_order = find_cust_order_by_id(id).await.unwrap();
	let name = find_cust_order_indexed_name(cust_order.name).await.unwrap();
	cust_order.name = name;
	add_cust_order(cust_order).await
}

pub async fn count_search_cust_orders_by_fields_records(cust_order_query_request:CustOrderQueryRequest) ->Result<CountNum, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.count_search_cust_orders_by_fields_records(cust_order_query_request).await
}

pub async fn delete_all_cust_orders(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let cust_orders = &app_state.await.context.cust_orders;
	let vecter_iterator = ids.iter();
	for cust_order_id in vecter_iterator {
		let _ = cust_orders.delete_cust_order(*cust_order_id).await;
	}
	Ok(0)
}

pub async fn delete_cust_order(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.delete_cust_order(id).await
}

pub async fn find_cust_order_by_id(id:i64) ->Result<CustOrderBean, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.find_cust_order_by_id(id).await
}

pub async fn find_cust_order_by_name(name:String) ->Result<CustOrderBean, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.find_cust_order_by_name(name).await
}

pub async fn list_active_cust_orders() ->Result<Vec<CustOrderBean>, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.list_active_cust_orders().await
}

pub async fn list_all_cust_orders() ->Result<Vec<CustOrderBean>, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.list_all_cust_orders().await
}

pub async fn search_cust_orders_by_fields(cust_order_query_request:CustOrderQueryRequest) ->Result<Vec<CustOrderBean>, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.search_cust_orders_by_fields(cust_order_query_request).await
}

pub async fn search_cust_orders_by_fields_by_page(cust_order_query_request:CustOrderQueryRequest) ->Result<Vec<CustOrderBean>, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.search_cust_orders_by_fields_by_page(cust_order_query_request).await
}

pub async fn soft_delete_all_cust_orders(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let cust_orders = &app_state.await.context.cust_orders;
	let vecter_iterator = ids.iter();
	for cust_order_id in vecter_iterator {
		let _ = cust_orders.soft_delete_cust_order(*cust_order_id).await;
	}
	Ok(0)
}

pub async fn soft_delete_cust_order(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.soft_delete_cust_order(id).await
}

pub async fn toggle_cust_order(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.toggle_cust_order(id).await
}

pub async fn toggle_one_cust_order(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	let cust_orders = &app_state.await.context.cust_orders;
	let cust_order = cust_orders.find_cust_order_by_id(id).await.unwrap();
	if cust_order.active == false {
		let _ = cust_orders.toggle_cust_order(id).await;
	} else {
		let count = cust_orders.count_active_cust_order_records().await.unwrap();
		if count.count_num > 1 {
			let _ = cust_orders.toggle_cust_order(id).await;
		}
	}
	Ok(0)
}

pub async fn update_cust_order(cust_order:CustOrderBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.update_cust_order(cust_order).await
}

pub async fn find_cust_order_indexed_name(name:String) ->Result<String, Error> {
	let mut short_name = name.clone();
	if name.contains("_") && is_digit(name.substring(right_find_under_line(name.clone())+1,name.chars().count()).to_string()) {
		short_name = name.substring(0,right_find_under_line(name.clone())).to_string();
	}

	let app_state = init_db();
	let cust_orders = app_state.await.context.cust_orders.find_cust_order_indexed_name(short_name.clone()).await.unwrap();

	let mut index_num = 1;
	for cust_order in cust_orders {
		let name2 = cust_order.name;
		if name2.contains("_") && is_digit(name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).to_string()) {
			let index = name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).parse::<i32>().unwrap();
			if index >= index_num {
				index_num = index + 1;
			}
		}
	}

	let index_name = short_name.clone() + "_" + &index_num.to_string();
	Ok(index_name)
}

pub async fn select_order_list(status_id:i64,pageNumber:i32) ->Result<Vec<CustOrderBean>, Error> {
    let pageSize:i32 = 10;
   	let app_state = init_db();
	let list = app_state.await.context.cust_orders.select_order_list(status_id, pageNumber, pageSize).await.unwrap();
    Ok(list)
}

pub async fn get_order_count(status_id:i64) -> Result<CountNum, Error> {
    let app_state = init_db();
	app_state.await.context.cust_orders.get_order_count(status_id).await
}

pub async fn commit_cust_order(cust_order:CustOrderBean,order_items:Vec<OrderItemBean>) -> Result<bool, Error> {
    let success = service_validate_order_items(order_items.clone()).await.unwrap();
    if success == false {
        return Ok(false);
    }
    let app_state = init_db();
	app_state.await.context.cust_orders.commit_cust_order(cust_order,order_items.clone()).await
}

pub async fn list_my_cust_orders(user_id:i64) ->Result<Vec<CustOrderBean>, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.list_my_cust_orders(user_id).await
}

pub async fn update_cust_order_status(id:i64, status_id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.update_cust_order_status(id,status_id).await
}

pub async fn delete_cust_order_with_order_items(cust_order_id:i64) ->Result<bool, Error> {
	let app_state = init_db();
	app_state.await.context.cust_orders.delete_cust_order_with_order_items(cust_order_id).await
}
